weeklyPublishedVariantsReport

- 🇬🇧 English
- 🇮🇹 Italiano
Function Names: weeklyPublishedVariantsReport / testWeeklyPublishedVariantsReport
Author: Domenico Cerone Creation Date: 02/10/2025
Last Reviewer: Domenico Cerone
Trigger: Scheduler (onSchedule - every Monday at 17:00) / HTTPS (onRequest)
Purpose: Executes a weekly notification (every Monday at 17:00) to monitor SKUs in 'In Pubblicazione' and 'Rev. Cliente' status for each CatalogOrder (aggregating all batches). Sends summary emails only to Validators and Clients associated with specific CatalogOrders, avoiding generic notifications to the entire system.
Detailed Functionality​
This Firebase Function performs the following operations in sequence:
1. MainBrands Data Collection​
- Retrieves all brands from the
MainBrandscollection in the database - For each brand, processes all associated variants to count status occurrences
2. Variants Grouping and Counting​
- For each brand, groups
Variantsby batch field - Counts variants with status 'In Pubblicazione' (
skuPublication) - Counts variants with status 'Rev. Cliente' (
skuRevClient) - Creates brand/batch combinations for reporting
3. Profile Filtering and Brand Association​
- Retrieves only profiles with role 'Validator' and 'Cliente' from
Profilescollection - Filters profiles by brand using the
list_main_brandsfield - NEW: Checks
blockEmailarray to exclude users who have blocked 'weeklyPublishedVariantsReport' notifications - Associates each user with their specific brands for targeted notifications
4. NOTIFICATION PREFERENCE CHECK​
- NEW FEATURE: Before sending any email, the function checks the recipient's profile for notification preferences
- Check Field:
notification_types.weeklyPublishedVariantsReportin the user's profile - Logic: If
notification_types.weeklyPublishedVariantsReportisfalse, the email is NOT sent - Default Behavior: If the field is missing or
true, the email is sent normally - Granular Control: Users can enable/disable specific notification types without affecting others
- Profile Field:
notification_typesis a map containing boolean values for each notification type - Example:
notification_types: { weeklyPublishedVariantsReport: false }disables this notification
5. Conditional Email Sending Logic​
Emails are sent ONLY if:
- There are SKUs to report (
skuPublication> 0 ORskuRevClient> 0) - There are recipients for that specific brand
- Brand has associated Validators or Clients in their
list_main_brands - NEW: Recipients have NOT blocked 'weeklyPublishedVariantsReport' in their
blockEmailarray
6. Email Configuration and Recipients​
- Primary Recipients (TO): Validators who have the specific brand in their
list_main_brandsand have NOT blocked 'weeklyPublishedVariantsReport' - Copy Recipients (CC): Clients who have the specific brand in their
list_main_brandsand have NOT blocked 'weeklyPublishedVariantsReport' - Email Service: ZeptoMail with predefined template (
MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.afbab030-45ff-11f0-832a-66e0c45c7bae.1975a0d02b3) - Fallback: Nodemailer for error cases
7. Template Data Variables​
The email template receives these merge variables:
brandName: Name of the brandbatch: Batch identifierskuPublication: Number of SKUs in 'In Pubblicazione' statusskuRevClient: Number of SKUs in 'Rev. Cliente' status
8. Email Blocking System​
- NEW FEATURE: Users can block specific notifications using the
blockEmailarray in their profile - Blocking Mechanism: Users add 'weeklyPublishedVariantsReport' to their
blockEmailarray to opt-out - Automatic Exclusion: Users with blocked notifications are automatically excluded from email sending
- Granular Control: Users can block individual notification types without affecting other notifications
- Profile Field:
blockEmailis an array of strings containing notification names to block - Example:
blockEmail: ["weeklyPublishedVariantsReport", "monthlyReport"]blocks both notifications
9. Automatic Scheduling​
- Schedule: Every Monday at 17:00 CET (
0 17 * * 1) - Region: europe-central2
- Timezone: Europe/Rome
10. Testing Function​
- HTTP Endpoint:
testMonthlyPublishedVariantsReport - Method: GET (no parameters required)
- Purpose: Manual testing with detailed JSON response
- Behavior: Identical logic but returns comprehensive debugging information
Input (Payload):​
For weeklyPublishedVariantsReport (Scheduled):
Automatic execution - no input required
For testMonthlyPublishedVariantsReport (HTTP):
Method: GET
No body parameters required
Output (Success):​
{
"success": true,
"message": "🧪 Report TEST generato con successo",
"dryRunMode": false,
"timestamp": "2024-01-19T17:00:00.000Z",
"statistics": {
"totalCatalogOrders": 5,
"catalogOrdersWithSkuToReport": 3,
"totalProfiles": {
"validators": 3,
"clients": 2,
"blocked": 1
},
"emailResults": {
"sent": 3,
"skipped": 2,
"errors": 0
}
},
"results": [
{
"catalogOrderId": "catalog123",
"orderName": "Spring Collection 2024",
"skuPublication": 15,
"skuRevClient": 3
}
],
"profilesByCatalogOrder": {
"catalog123": {
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
},
"emailResults": [
{
"catalogOrder": "Spring Collection 2024",
"status": "sent",
"skuPublication": 15,
"skuRevClient": 3,
"recipients": {
"to": 1,
"cc": 1,
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
}
]
}
This output is returned in the HTTP response (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport
Test with Emulator:
- Start the Firebase emulator:
firebase emulators:start --only functions - Ensure you're using Node.js version 20:
nvm use 20 - Test with curl:
curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport"
Test with Dry Run Mode:
curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport?dryRun=true"
Postman Testing:
- Method: GET
- URL: http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport
- Query Parameters: Optional
dryRun=truefor testing without sending real emails - Headers: None required
- Body: Not required
Deploy Command:​
firebase deploy --only functions:weeklyPublishedVariantsReport
Production URL:​
weeklyPublishedVariantsReport: https://europe-central2-arshades-7e18a.cloudfunctions.net/weeklyPublishedVariantsReport
Note: This function runs automatically every Monday at 17:00. The URL is for reference only as scheduled functions cannot be called directly.
testWeeklyPublishedVariantsReport: Not deployed to production (test function only)
Function Names: weeklyPublishedVariantsReport / testWeeklyPublishedVariantsReport
Autore: Domenico Cerone Data di creazione: 02/10/2025
Last Reviewer: Domenico Cerone
Trigger: Scheduler (onSchedule - ogni lunedì alle 17:00) / HTTPS (onRequest)
Purpose: Esegue una notifica settimanale (ogni lunedì alle 17:00) per monitorare gli SKU in stato 'In Pubblicazione' e 'Rev. Cliente' per ogni CatalogOrder (aggregando tutti i batch). Invia email di riepilogo solo ai Validator e Clienti associati a specifici CatalogOrders, evitando notifiche generiche a tutto il sistema.
Funzionamento Dettagliato​
Questa Firebase Function esegue le seguenti operazioni in sequenza:
1. Raccolta Dati CatalogOrders​
- Recupera tutti i CatalogOrders con status='working_3d_assets' e type='order_3d_asset' dal database
- Per ogni CatalogOrder, elabora tutte le varianti associate per contare le occorrenze di stato
2. Raggruppamento e Conteggio Varianti​
- Per ogni CatalogOrder, conta TUTTE le
Variants(aggregando tutti i batch) che hanno catalogOrdersRef corrispondente all'ID del CatalogOrder - Conta le varianti con stato 'In Pubblicazione' (
skuPublication) - Conta le varianti con stato 'Rev. Cliente' (
skuRevClient) - Crea totali per CatalogOrder (non per batch)
3. Filtro Profili e Associazione CatalogOrder​
- Recupera solo i profili con ruolo 'Validator' e 'Cliente' dalla collezione
Profiles - Filtra i profili per CatalogOrder utilizzando il campo
list_3d_catalogOrders - NUOVO: Controlla l'array
blockEmailper escludere utenti che hanno bloccato le notifiche 'weeklyPublishedVariantsReport' - Associa ogni utente ai suoi CatalogOrders specifici per notifiche mirate
4. CONTROLLO PREFERENZE NOTIFICHE​
- NUOVA FUNZIONALITÀ: Prima di inviare qualsiasi email, la funzione controlla le preferenze di notifica del destinatario nel suo profilo
- Campo di Controllo:
notification_types.weeklyPublishedVariantsReportnel profilo dell'utente - Logica: Se
notification_types.weeklyPublishedVariantsReportèfalse, l'email NON viene inviata - Comportamento Predefinito: Se il campo è mancante o
true, l'email viene inviata normalmente - Controllo Granulare: Gli utenti possono abilitare/disabilitare tipi specifici di notifica senza influenzare gli altri
- Campo Profilo:
notification_typesè una mappa contenente valori booleani per ogni tipo di notifica - Esempio:
notification_types: { weeklyPublishedVariantsReport: false }disabilita questa notifica
5. Logica di Invio Email Condizionale​
Le email vengono inviate SOLO se:
- Ci sono SKU da segnalare (
skuPublication> 0 OskuRevClient> 0) - Ci sono destinatari per quel CatalogOrder specifico
- Il CatalogOrder ha Validator o Clienti associati nel loro
list_3d_catalogOrders - NUOVO: I destinatari NON hanno bloccato 'weeklyPublishedVariantsReport' nel loro array
blockEmail
6. Configurazione Email e Destinatari​
- Destinatari Principali (TO): Validator che hanno il CatalogOrder specifico nel loro
list_3d_catalogOrderse NON hanno bloccato 'weeklyPublishedVariantsReport' - Destinatari in Copia (CC): Clienti che hanno il CatalogOrder specifico nel loro
list_3d_catalogOrderse NON hanno bloccato 'weeklyPublishedVariantsReport' - Servizio Email: ZeptoMail con template predefinito (
MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.afbab030-45ff-11f0-832a-66e0c45c7bae.1975a0d02b3) - Fallback: Nodemailer per casi di errore
7. Variabili Dati Template​
Il template email riceve queste variabili di merge:
idCatalogOrders: ID del CatalogOrderorderName: Nome del CatalogOrderskuPublication: Numero di SKU in stato 'In Pubblicazione' (totale di tutti i batch)skuRevClient: Numero di SKU in stato 'Rev. Cliente' (totale di tutti i batch)
8. Sistema di Blocco Email​
- NUOVA FUNZIONALITÀ: Gli utenti possono bloccare notifiche specifiche utilizzando l'array
blockEmailnel loro profilo - Meccanismo di Blocco: Gli utenti aggiungono 'weeklyPublishedVariantsReport' al loro array
blockEmailper disattivare - Esclusione Automatica: Gli utenti con notifiche bloccate vengono automaticamente esclusi dall'invio email
- Controllo Granulare: Gli utenti possono bloccare singoli tipi di notifica senza influenzare altre notifiche
- Campo Profilo:
blockEmailè un array di stringhe contenente i nomi delle notifiche da bloccare - Esempio:
blockEmail: ["weeklyPublishedVariantsReport", "monthlyReport"]blocca entrambe le notifiche
9. Pianificazione Automatica​
- Programma: Ogni lunedì alle 17:00 CET (
0 17 * * 1) - Regione: europe-central2
- Fuso Orario: Europe/Rome
10. Funzione di Test​
- Endpoint HTTP:
testWeeklyPublishedVariantsReport - Metodo: GET con parametro opzionale
dryRun=true - Scopo: Test manuale con risposta JSON dettagliata
- Comportamento: Logica identica ma restituisce informazioni complete di debugging
- Modalità Dry Run: Quando
dryRun=true, le email vengono simulate ma non inviate realmente
Input (Payload):​
Per weeklyPublishedVariantsReport (Schedulata):
Esecuzione automatica - nessun input richiesto
Per testWeeklyPublishedVariantsReport (HTTP):
Metodo: GET
Parametro query opzionale: dryRun=true (per test senza inviare email reali)
Output (Success):​
{
"success": true,
"message": "🧪 Report TEST generato con successo",
"dryRunMode": false,
"timestamp": "2024-01-19T17:00:00.000Z",
"statistics": {
"totalCatalogOrders": 5,
"catalogOrdersWithSkuToReport": 3,
"totalProfiles": {
"validators": 3,
"clients": 2,
"blocked": 1
},
"emailResults": {
"sent": 3,
"skipped": 2,
"errors": 0
}
},
"results": [
{
"catalogOrderId": "catalog123",
"orderName": "Spring Collection 2024",
"skuPublication": 15,
"skuRevClient": 3
}
],
"profilesByCatalogOrder": {
"catalog123": {
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
},
"emailResults": [
{
"catalogOrder": "Spring Collection 2024",
"status": "sent",
"skuPublication": 15,
"skuRevClient": 3,
"recipients": {
"to": 1,
"cc": 1,
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
}
]
}
Questo output viene restituito nella risposta HTTP (Postman, browser, curl)
Testing​
URL (if HTTPS): http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport
Test con Emulator:
- Avviare l'emulatore Firebase:
firebase emulators:start --only functions - Assicurarsi di usare Node.js versione 20:
nvm use 20 - Testare con curl:
curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport"
Test con Modalità Dry Run:
curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport?dryRun=true"
Postman Testing:
- Metodo: GET
- URL: http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport
- Parametri Query: Opzionale
dryRun=trueper test senza inviare email reali - Headers: Nessuno richiesto
- Body: Non richiesto
Deploy Command:​
firebase deploy --only functions:weeklyPublishedVariantsReport
URL di Produzione:​
weeklyPublishedVariantsReport: https://europe-central2-arshades-7e18a.cloudfunctions.net/weeklyPublishedVariantsReport
Nota: Questa funzione viene eseguita automaticamente ogni lunedì alle 17:00. L'URL è solo di riferimento poiché le funzioni schedulate non possono essere chiamate direttamente.
testWeeklyPublishedVariantsReport: Non deployata in produzione (solo funzione di test)